<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	 
	public function __construct()
	{
		parent::__construct();
		$this->load->model('login_model');
		$this->load->model('registration_model');
		$this->load->model('create_task_model');
		$this->load->model('retrieve_reminder_data_model');
	}
	
	public function index()
	{
		$data['msg'] = '';
		$this->load->view('login_view',$data);
	}
	
	public function new_account()
	{
		$data['msg'] = '';
		$this->load->view('registration_view',$data);
	}
	
	public function new_task()
	{
		$data['msg'] = '';
		$this->load->view('create_task_view',$data);
	}	
	public function register_user()
	{
		$reg_data['username'] = $this->input->post('username');
		$reg_data['password'] = $this->input->post('password');
		$reg_data['email'] = $this->input->post('email');
		$reg_data['phone'] = $this->input->post('phone');
		
		if($reg_data['username'] == null || $reg_data['password'] == null || $reg_data['email'] == null){
			$data['msg'] = 'Username, password and email are required. ';
			$this->load->view('registration_view',$data);
		}
		else{
			$data['msg'] = $this->registration_model->register_user($reg_data);
			if($data['msg'] == 1){
				$data['msg'] = "Your Account has been created. Please log in ";
				$this->load->view('login_view', $data);
			}
			else{
				$data['msg'] = "Your Account could not be created. We apologize for the inconvenience. Please try again ";
				$this->load->view('registration_view', $data);
			}
		}
	}
	
	function r_edit()
	{
		//echo "Redit ".$_POST['id'];
		$this->create_task_model->edit($_POST['id']);
	}
	
	function delete()
	{
		//echo "Rdel ".$_POST['id'];
		$data['msg'] = $this->create_task_model->delete($_POST['id']);
		$data['result'] = $this->create_task_model->get_task_list(7);
		$this->load->view('task_list_view', $data);
	}

	function nr_edit()
	{
		//echo "NRedit ".$_POST['id'];
		$this->create_task_model->edit($_POST['id']);
	}
	

	public function login()
	{
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		
		$data['msg'] = $this->login_model->validate($username, $password);
			
		if($data['msg'] == "Successful"){
			$data['msg'] = "";
			$data['result'] = $this->create_task_model->get_task_list(7);
			$this->load->view('task_list_view', $data);
		}
		else{ 
				$data['msg'] = "Username/Password does not exist"; 
				$this->load->view('login_view', $data); 
			}
	}
	
	public function create_task()
	{
		$new_task_array['task'] = $this->input->post('task');
		$new_task_array['task_type'] = $this->input->post('task_type');
		$new_task_array['date'] = $this->input->post('date');
		$new_task_array['nrtime'] = $this->input->post('nrtime');
		$new_task_array['start_date'] = $this->input->post('start_date');
		$new_task_array['end_date'] = $this->input->post('end_date');
		$new_task_array['rtime'] = $this->input->post('rtime');
		$days_array = $this->input->post('day');
		$emailphone_array = $this->input->post('email_phone');
		
		//var_dump($days_array);
		//var_dump($new_task_array);
		//var_dump($emailphone_array);
		echo "<br/>";
		$message = $this->create_task_model->add_task($new_task_array, $days_array, $emailphone_array);
		
		switch($message){
			case 'pass-r':	$data['msg'] = "Recurring reminder added successfully.";
							break;
			case 'pass-nr':	$data['msg'] = "Non Recurring reminder added successfully.";
							break;
			case 'fail':	$data['msg'] = "Reminder could not be added to the database. Sorry!";
							break;
			case 'fail-r-rnf': $data['msg'] = "Database inconsistency. Please contact tech support.";
							break;							
		}
		$data['result'] = $this->create_task_model->get_task_list(7);
		
		$this->load->view('task_list_view', $data); 
	}
	
	public function send_reminders(){
		$this->retrieve_reminder_data_model->retrieve_data();
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */